﻿Public Class frmSettings
    Private m_controller As IController
    Private m_outputDev As IControlOutput
    Private m_controllerList As List(Of IControllerFactory)
    Private m_wasConnectedLast As Boolean

    Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        Me.Show()
    End Sub

    Private Sub frmSettings_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        m_controllerList = New List(Of IControllerFactory)()
        'm_outputDev = New StandardControlOutput()

        ResetController()
    End Sub

    Private Sub ResetController()
        With comboBoxController.Items
            .Clear()
            m_controllerList.Clear()

            For Each controller In (New ControllerFactoryEnumerable())
                .Add(controller.Description)
                m_controllerList.Add(controller)
            Next
        End With
    End Sub

    Private Sub clockTimer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clockTimer.Tick
        If (Not (m_controller Is Nothing)) And Not ((m_outputDev Is Nothing)) Then
            If m_controller.IsConnected() Then
                m_wasConnectedLast = True

                Dim pt As PointF = m_controller.GetLeftStickPos()
                pt = New PointF(pt.X * 2.0 - 1.0, pt.Y * 2.0 - 1.0)

                m_outputDev.MoveMouse(pt)
            Else
                If m_wasConnectedLast Then
                    m_wasConnectedLast = False

                    'Just disconnected.

                    Me.Show()
                End If
            End If
        End If
    End Sub

    Private Sub comboBoxController_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comboBoxController.SelectedIndexChanged
        If Not m_controller Is Nothing Then
            m_controller.Dispose()
            m_controller = Nothing
        End If

        If Not comboBoxController.SelectedIndex = -1 Then
            m_controller = m_controllerList.Item(comboBoxController.SelectedIndex).CreateController()

            If m_controller.GetType() Is GetType(VirtualController) Then
                m_outputDev = New VirtualControlOutput()
                With CType(m_outputDev, VirtualControlOutput)
                    .Parent = CType(m_controller, VirtualController).Parent
                End With
            End If
        End If
    End Sub
End Class